package com.yanze.schoolinformation.adapter;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yanze.schoolinformation.BR;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.model.bean.NewsModule;
import com.yanze.schoolinformation.utils.ToastUtils;
import com.yanze.schoolinformation.view.home.news.SchoolNewsActivity;
import com.yanze.schoolinformation.view.webview.WebViewActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by MrZheng on 2018/3/21.
 */

public class HomeItemRecyclerAdapter extends RecyclerView.Adapter {
    private List<NewsModule> mDatas;
    public final int HEAD_ITEM = 4;
    public final int TOP_ITEM = 0;
    public final int TITLE_ITEM = 1;
    public final int NORMAL_ITEM = 2;

    private Context mContext;

    private View HeadView;
    private View FootView;

    public void addHeadView(View headView) {
        HeadView = headView;
    }

    public HomeItemRecyclerAdapter(Context context) {
        mContext = context;
        mDatas = new ArrayList<>();
    }

    public class CurrViewHolder extends RecyclerView.ViewHolder {
        public ViewDataBinding dataBinding;

        public CurrViewHolder(ViewDataBinding dataBinding) {
            super(dataBinding.getRoot());
            this.dataBinding = dataBinding;
        }
    }

    public class HeadViewHolder extends RecyclerView.ViewHolder {
        public HeadViewHolder(View itemView) {
            super(itemView);
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewDataBinding binding = null;
        if (viewType == TOP_ITEM) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_home_slogan, parent, false);
        } else if (viewType == TITLE_ITEM) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_news_title, parent, false);
        } else if (viewType == NORMAL_ITEM) {
            binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_news, parent, false);
        } else if (viewType == HEAD_ITEM) {
            return new HeadViewHolder(HeadView);
        }
        CurrViewHolder holder = new CurrViewHolder(binding);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position == 0) {
            return;
        }
        if (HeadView != null && position != 0) {
            position = position - 1;
        }
//        position = position - 1;
        ((CurrViewHolder) holder).dataBinding.setVariable(BR.news, mDatas.get(position));
        ((CurrViewHolder) holder).dataBinding.setVariable(BR.adapter, this);
        ((CurrViewHolder) holder).dataBinding.executePendingBindings();
    }

    @Override
    public int getItemCount() {
        return HeadView == null ? mDatas.size() : mDatas.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (HeadView != null && position == 0) {
            return HEAD_ITEM;
        }
        position = position - 1;
        int type = mDatas.get(position).getType();
        if (type == TOP_ITEM) {
            return TOP_ITEM;
        } else if (type == TITLE_ITEM) {
            return TITLE_ITEM;
        } else if (type == NORMAL_ITEM) {
            return NORMAL_ITEM;
        }
        return 0;
    }

    //插入单个条目
    public void addItem(NewsModule data) {
        if (data != null) {
            mDatas.add(data);
            notifyItemInserted(mDatas.size() - 1);
        }
    }

    //添加一个集合的条目
    public void setAllItem(List<NewsModule> datas) {
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clearItems() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    //点击事件
    public void newsDetail(String link) {
        if (link.startsWith("http")) {
            WebViewActivity.loadUrl(mContext, link, "正在加载...");
        } else {
            SchoolNewsActivity.start(mContext,link);
        }
    }
}
